var express = require('express');
var router = express.Router();
const Blog = require('../connet/mongod')
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/banner', async (req, res)=>{
  try {
    const data = await Blog.Banner.find()
    res.status(200).send(data)
  } catch (error) {
    console.log(error, '错误');
  }
});
// 新增
router.post('/addbanner', async (req, res)=>{
  try {  
    const imgobj = {url: req.body.image, title: 'banner'}
    console.log('req.body.image', imgobj)
    const data = await Blog.Banner.create(imgobj)
    res.status(200).send({data, isok: true });  
  } catch (error) {  
    console.error('Error inserting banners:', error);  
    res.status(500).send({ message: 'An error occurred while inserting banners' });  
  }
})
// 删除
router.post('/delbanner', async function(req, res){
    const _id = req.body.id
    const data = await Blog.Banner.deleteOne(_id);
    res.status(200).send({data,isok:true})
  })
module.exports = router;
